﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Input;

using ScytaleDecrypt.Model;

namespace ScytaleDecrypt.ViewModel
{
    internal class Main : ViewModelBase {
        public ImageWrapper Image { get; set; }

        public Message Message { get; set; }

        private Dictionary Dictionary { get; set; }

        public String FilePath {
            get {
                return Image.FilePath;
            }
            set {
                if (value != null) {
                    Image.FilePath = value;
                    OnPropertyChanged("FilePath");
                }

            }
        }

        public String FileText {
            get {
                return Message.OriginalMessage;
            }
            set {
                if (value != null) {
                    Message.OriginalMessage = value;
                    OnPropertyChanged("FileText");
                }
            }
        }

        private string decryptedText;

        public string DecryptedText {
           get {
               return decryptedText;
           }
            set {
                if (value != null) {
                    decryptedText = value; 
                    OnPropertyChanged("DecryptedText");
                };
            }
        }

        public string SelectedLanguage {
            get {
                return Dictionary.CurrentLanguage;
            }
            set {
                if (value != null) {
                    Dictionary.CurrentLanguage = value;
                    LoadDictionaryLanguage();
                    OnPropertyChanged("SelectedLanguage");
                }
            }
        }

        public ObservableCollection<string> DecryptedMessages { get; set; }

        public ObservableCollection<string> Languages { get; set; } 

        public ICommand SetFilePathCommand { get; set; }

        public ICommand SetFileTextCommand { get; set; }

        public ICommand DecryptMessageCommand { get; set; }

        public Main() {
            Message = new Message();
            Image = new ImageWrapper();
            Dictionary = Dictionary.GetInstance;
            InitializeCollections();
            InitialzeCommands();
        }

        private void SetFilePath() {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            bool? result = dlg.ShowDialog();

            if (result == true) {
                FilePath = dlg.FileName;
                Image.FilePath = FilePath;
            }
        }

        private void SetFileText() {
            FileText = Image.GetText();
        }

        private void DecryptMessage() {
            CheckDecryptionParameters();
            List<String> decryptedMessagesList = Message.Decrypt();
            FillObserverableCollection(decryptedMessagesList);
        }

        private void LoadDictionaryLanguage() {
            Dictionary.GetInstance.ConstructWordList();
        }

        private void InitialzeCommands() {
                SetFilePathCommand = new RelayCommand(param => SetFilePath());
                SetFileTextCommand = new RelayCommand(param => SetFileText());
                DecryptMessageCommand = new RelayCommand(param => DecryptMessage());
     
        }

        private void InitializeCollections() {
            DecryptedMessages = new ObservableCollection<string>();
            Languages = new ObservableCollection<string>();
            Languages.Add("german");
            Languages.Add("english");
        }

        private void CheckDecryptionParameters()
        {
            if (FileText == null) {
                throw new Exception("Text must not be null!");
            }
        }

        private void FillObserverableCollection(List<String> decryptedMessagesList)
        {
            if (decryptedMessagesList != null) {
                DecryptedMessages = new ObservableCollection<string>(decryptedMessagesList);
                OnPropertyChanged("DecryptedMessages");
            }
            else {
                DecryptedMessages.Clear();
                DecryptedMessages.Add("No Message found!");
            }
        }
    }

}
